/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.wordplat.quickstart.slice;

import com.wordplat.quickstart.ResourceTable;
import com.wordplat.quickstart.ability.MainAbility;
import com.wordplat.quickstart.adapter.TextAdapter;
import com.wordplat.quickstart.base.BaseAbilitySlice;
import com.wordplat.quickstart.utils.ToastViewDialog;

import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.KeyEvent;

/**
 * MainSlice
 *
 * @since 2021-05-08
 */
public class MainSlice extends BaseAbilitySlice {
    /**
     * HiLogLabel
     */
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainAbilitySlice");
    private static final int NUM_16 = 16;
    private static final int NUM_255 = 255;
    private static final int NUM_12 = 12;
    private static final int NUM_0 = 0;
    private static final int NUM_2000 = 2000;
    private ListContainer textList = null;
    private TextAdapter textAdapter;
    /**
     * 点击两次退出
     */
    private long exitTime = 0L;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_main);

        initViews();
    }

    /**
     * 初始化view
     */
    private void initViews() {
        textList = (ListContainer) findComponentById(ResourceTable.Id_textList);

        textAdapter = new TextAdapter(this);
        textList.setItemProvider(textAdapter);

        for (int resultI = NUM_0; resultI < NUM_16; resultI++) {
            String result = Integer.toHexString(NUM_255 - resultI * NUM_12);
            HiLog.info(LABEL, "##d onCreate: " + result);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEY_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > NUM_2000) {
            ToastViewDialog.toast(getApplicationContext(), "再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            this.exit();
            terminateAbility();
        }
    }
}
